package com.wjpeng.wms.service.impl;

import com.wjpeng.wms.domain.OrderBillItem;
import com.wjpeng.wms.domain.Orderbill;
import com.wjpeng.wms.mapper.OrderBillItemMapper;
import com.wjpeng.wms.mapper.OrderbillMapper;
import com.wjpeng.wms.query.OrderBillQueryObject;
import com.wjpeng.wms.query.QueryObject;
import com.wjpeng.wms.service.IOrderbillService;
import com.wjpeng.wms.util.PageResult;
import com.wjpeng.wms.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Collections;
import java.util.Date;
import java.util.List;

@Service
public class OrderbillServiceImpl implements IOrderbillService {
    @Autowired
    private OrderbillMapper billMapper;

    @Autowired
    private OrderBillItemMapper itemMapper;

    @Override
    public void saveOrUpdate(Orderbill d) {
        if(d.getId() == null){
            saveBill(d);
        }else{
            updateBill(d);
        }


    }

    //保存订单
    private void saveBill(Orderbill entity){
        //1:设置录入人
        entity.setInputUser(UserContext.getCurrentUser());
        //2:设置录入时间
        entity.setInputTime(new Date());
        //3:设置单据状态
        entity.setStatus(Orderbill.NORMAL);
        //4:定义两个变量,来记录当前单据的总数量和总金额
        BigDecimal totalNumber = BigDecimal.ZERO;
        BigDecimal totalAmount = BigDecimal.ZERO;

        //5:设置明细数据
        for (OrderBillItem item : entity.getItems()) {
            //4.1:计算单据的小计
            BigDecimal costPrice = item.getCostPrice();
            BigDecimal number = item.getNumber();
            BigDecimal amount = costPrice.multiply(number)
                    .setScale(2, RoundingMode.HALF_UP);
            item.setAmount(amount);
            //4.2:叠加总数量和总计
            totalNumber = totalNumber.add(number);
            totalAmount = totalAmount.add(amount);
        }
        //6:设置单据的总价和总数量
        entity.setTotalNumber(totalNumber);
        entity.setTotalAmount(totalAmount);
        //7:保存单据到数据库中
        billMapper.insert(entity);
        //8:保存明细
        for (OrderBillItem item : entity.getItems()) {
            item.setBillId(entity.getId());
            //保存明细到数据库中
            itemMapper.insert(item);
        }
    }

    //更新单据
    private void updateBill(Orderbill d) {
        //根据单据的ID查询完整的单据信息
        Orderbill old = billMapper.selectByPrimaryKey(d.getId());
        //检验当前单据的状态,未审核才能更新
            if(old.getStatus() == Orderbill.NORMAL){
                //核心,重新计算明细小计,单据的总数量和总价格
                //1:删除当前单据所有的明细
                itemMapper.deleteItemByBillId(old.getId());

                //2:定义变量来记录总数和总价
                BigDecimal totalNumber = BigDecimal.ZERO;
                BigDecimal totalAmount = BigDecimal.ZERO;

                //3:计算单据的总价和总数量
                    for (OrderBillItem item : d.getItems()) {
                        BigDecimal number = item.getNumber();
                        BigDecimal costPrice = item.getCostPrice();
                        BigDecimal amount = costPrice.multiply(number).
                                setScale(2, RoundingMode.HALF_UP);
                        //3.1:保存明细的总价
                        item.setAmount(amount);
                        //3.2叠加总数量和总金额
                        totalNumber = totalNumber.add(number);
                        totalAmount = totalAmount.add(amount);
                        //4:保存明细
                        item.setBillId(old.getId());
                        itemMapper.insert(item);
                    }
                    old.setTotalNumber(totalNumber);
                    old.setTotalAmount(totalAmount);

                //回填传递的订单数据
                old.setSn(d.getSn());
                old.setVdate(d.getVdate());
                old.setSupplier(d.getSupplier());
                //5:更新订单
                billMapper.updateByPrimaryKey(old);
            }

    }

    @Override
    public void delete(Long id) {
        //根据单据的ID查询完整的单据信息
        Orderbill old = billMapper.selectByPrimaryKey(id);
        //检验当前单据的状态,未审核才能更新
        if(old.getStatus() == Orderbill.NORMAL){
            //删除当前单据的明细,在删除订单
            itemMapper.deleteItemByBillId(id);
            billMapper.deleteByPrimaryKey(id);

        }


    }

    @Override
    public Orderbill get(Long id) {
        return billMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Orderbill> list() {
        return billMapper.selectAll();
    }

    @Override
    public PageResult result(OrderBillQueryObject qo) {
        Integer count = billMapper.queryCount(qo);
        if(count == 0){
            return new PageResult(Collections.EMPTY_LIST,0,1,0);
        }
        return new PageResult(billMapper.queryLimit(qo),count,qo.getCurrentPage(),qo.getPageSize());
    }

    @Override
    public void audit(Long id) {
        //根据单据的ID查询完整的单据信息
        Orderbill old = billMapper.selectByPrimaryKey(id);
        //检验当前单据的状态,未审核的才能审核
        if(old.getStatus() == Orderbill.NORMAL){
            //核心:添加审批人,审核时间,修改单据状态
            old.setAuditUser(UserContext.getCurrentUser());
            old.setAuditTime(new Date());
            old.setStatus(Orderbill.AUDIT);
            billMapper.audit(old);
        }
    }
}
